package com.lguplus.servicepoi.request;

import org.jdom.Element;

import com.lguplus.servicepoi.exception.DBConnectionException;
import com.lguplus.servicepoi.exception.XMLValidationException;
import com.lguplus.servicepoi.transfer.POIReviewInfo;

/**
 * POI Review Regist Request
 */
public class ReviewRegistRequest extends Request {

	/** POI Code */
	private String poiCode;
	/** POI Review Information */
	private POIReviewInfo poiReviewInfo;
	
	public ReviewRegistRequest() {
		super();
		
		this.poiCode = "";
		this.poiReviewInfo = new POIReviewInfo();
	}

	/* (non-Javadoc)
	 * @see com.lguplus.servicepoi.request.Request#runXmlAdaptor()
	 */
	@Override
	public void runXmlAdaptor() throws XMLValidationException, DBConnectionException {
		// H-0. Analyze Header
		headerElemToObject();
		
		// B-1. Get Child Node : poi_info
		Element poiCodeE = this.bodyElem.getChild("poi_code");
		if(poiCodeE != null)
			if(poiCodeE.getText().trim().length() == 0)
				throw new XMLValidationException("poi_code" + XMLValidationException.VALUE_MSG, XMLValidationException.VALUE_CD);
			else{
				this.setPoiCode(poiCodeE.getText().trim());
				this.poiReviewInfo.setPoiCode(poiCodeE.getText().trim());
			}
		else
			throw new XMLValidationException("poi_code" + XMLValidationException.ELEMENT_MSG, XMLValidationException.ELEMENT_CD);
		
		// B-2. Get Child Node : grade
		Element gradeE = this.bodyElem.getChild("grade");
		if(gradeE != null){
			try{
				this.poiReviewInfo.setGrade(Double.parseDouble(gradeE.getText().trim()));
			}catch(Exception e){
				throw new XMLValidationException("grade" + XMLValidationException.TYPE_MSG, XMLValidationException.TYPE_CD);
			}
		}

		// B-3. Get Child Node : review
		Element reviewE = this.bodyElem.getChild("review");
		if(reviewE != null)
			if(reviewE.getText().trim().length() == 0)
				throw new XMLValidationException("review" + XMLValidationException.VALUE_MSG, XMLValidationException.VALUE_CD);
			else{
				this.poiReviewInfo.setReview(reviewE.getText().trim());
				if(this.poiReviewInfo.getReview().getBytes().length > 500)
					throw new XMLValidationException("review" + XMLValidationException.TYPE_MSG, XMLValidationException.TYPE_CD);
			}
		else
			throw new XMLValidationException("review" + XMLValidationException.ELEMENT_MSG, XMLValidationException.ELEMENT_CD);

		// B-4. Get Child Node : image
		Element imageE = this.bodyElem.getChild("image");
		if(imageE != null){
			this.poiReviewInfo.setImage(imageE.getText().trim());

			// B-5. Get Child Node : thumbnail_size
			Element thumbnailSizeE = this.bodyElem.getChild("thumbnail_size");
			if(thumbnailSizeE != null){
				String thumbnailSize = thumbnailSizeE.getText().trim();
				if(thumbnailSize.indexOf("*") > 0){
					this.poiReviewInfo.setThumbnailWidth(thumbnailSize.substring(0, thumbnailSize.indexOf("*")));
					this.poiReviewInfo.setThumbnailHeight(thumbnailSize.substring(thumbnailSize.indexOf("*") + 1));
				}else{
					throw new XMLValidationException("thumbnail_size" + XMLValidationException.TYPE_MSG, XMLValidationException.TYPE_CD);
				}
			}else
				throw new XMLValidationException("thumbnail_size" + XMLValidationException.ELEMENT_MSG, XMLValidationException.ELEMENT_CD);
		}

	}

	public String getPoiCode() {
		return poiCode;
	}

	public void setPoiCode(String poiCode) {
		this.poiCode = poiCode;
	}

	public POIReviewInfo getPoiReviewInfo() {
		return poiReviewInfo;
	}

	public void setPoiReviewInfo(POIReviewInfo poiReviewInfo) {
		this.poiReviewInfo = poiReviewInfo;
	}

	public void printBody(){
		System.out.println("poiCode = " + this.poiCode);
		this.poiReviewInfo.print();
	}
}
